package main

import (
	"log"
	"net"
	"server_cluster/common/config"
	//	"server_cluster/common/logger"
	"server_cluster/common/mysock"
	"server_cluster/common/protoc/pb"
	"time"
	//	"encoding/binary"
	"google.golang.org/protobuf/proto"
)

func main() {
	//	logger.InitLogger("gate-client", "debug")
	//	defer logger.CloseLogger()
	conn, err := net.Dial("tcp", "127.0.0.1:11888")
	if err != nil {
		log.Println("connect fail.")
		return
	}
	defer conn.Close()
	u := &pb.CS_Login{
		//Uid:   100000019,
		Uid:   437763,
		Token: "test",
	}
	data, _ := proto.Marshal(u)

	var result uint16
	mybuf := mysock.Create()
	mybuf.Command = config.CS_Login
	mybuf.Ext = 1
	result = mybuf.WriteOutByConn(conn, data)
	if result != config.OK {
		return
	}
	log.Printf("send command=%d,length=%d,code=%d,ext=%d,session=%d", mybuf.Command, mybuf.Data_length, mybuf.Code, mybuf.Ext, mybuf.Session)
	result = mybuf.ReadFromOutSide(conn)
	if result != config.OK {
		log.Println("cs_login read result", result)
		return
	}
	user := pb.SC_Login{}
	proto.Unmarshal(mybuf.Data[mysock.HEAD_LEN:mysock.HEAD_LEN+mybuf.Data_length], &user)
	session := user.GetSession()

	log.Printf("recv command=%d,length=%d,code=%d,ext=%d,uid=%d,session=%v", mybuf.Command, mybuf.Data_length, mybuf.Code, mybuf.Ext, mybuf.Session, session)

	test := &pb.CS_Create_Battle{
		GmFight:       true,
		BattleGroupId: 210203,
	}
	test_data, _ := proto.Marshal(test)
	mybuf.Command = config.CS_CREATE_BATTLE
	mybuf.Session = session
	mybuf.Ext++
	result = mybuf.WriteOutByConn(conn, test_data)
	if result != config.OK {
		return
	}
	log.Printf("send command=%d,length=%d,code=%d,ext=%d,session=%v", mybuf.Command, mybuf.Data_length, mybuf.Code, mybuf.Ext, mybuf.Session)

	/*
		test1 := &pb.CS_Battle_Action {
			Fid:1,
			Index:1,
			TimeMs:10,
			TriggerType:2,
			TriggerIndex:0,
			TargetType:1,
			TargetIndex:0,
			Card:1,
			Num:1,
		}
		test1_data,_ := proto.Marshal(test1)
		mybuf.Command = config.CS_BATTLE_ACTION
		mybuf.Session = session
		mybuf.Ext++
		result = mybuf.WriteOut(conn, test1_data)
		if result != config.OK {
			return
		}
	*/
	/*
		test := &pb.CS_Item_Action{
			Type:1,
		}
		test.Items = make([]*pb.Item_Req,1)
		test.Items[0] = &pb.Item_Req {
			Cell:4,
			Id:1051,
			Type:1,
			Num:1,
		}
		data,err = proto.Marshal(test)
		mybuf.Command = config.CS_ITEM_ACTION
		mybuf.Ext = 2
		result = mybuf.MyWriteClient(conn,data)
	*/ /*
		mybuf.Command = 9999
		mybuf.Ext++
		result = mybuf.MyWriteClient(conn,data)
		if result < 0 {
			log.Printf("socket write error.\n")
			return
		}*/
	for {
		result = mybuf.ReadFromOutSide(conn)
		if result != config.OK {
			if result == config.TIMEOUT {
				//logger.Info("time out...")
				continue
			}
			log.Println("read result = ", result)
			break
		}
		log.Printf("recv command=%d,length=%d,code=%d,ext=%d,uid=%d", mybuf.Command, mybuf.Data_length, mybuf.Code, mybuf.Ext, mybuf.Session)
		time.Sleep(2 * time.Second)
		//	time.Sleep(20*time.Second)
	}
	log.Println("stop...")
}
